﻿/*----------------------------------------------------------------
// Copyright (C) 2016 通通优品版权所有。
// 命名空间: TongTongMall.MallWeb.Core.Tenant.Services
// 类名：ConfigDomainService
// 功能描述：N/A
// 
// 创建标识：Roc.Lee(李鹏鹏) 2017/6/17 17:35:11 TEC-ROCLEE Roc.Lee

// 修改标识：
// 修改描述：
// 
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TEC.Public.Component.Api;
using TEC.Public.Infrastructure.Config;
using TongTongMall.Domain.Repositories.Repositories;
using TongTongMall.MallWeb.Core.Instance.Config;

namespace TongTongMall.MallWeb.Core.Tenant.Services
{
    public class ConfigDomainService : IConfigDomainService
    {
        public ConfigDomainService()
        {
        }
        public Task<T> GetConfig<T>(long _TenantId) where T : ITongTongMallConfigInfo
        {
            return Task.FromResult<T>(ConfigHelper.Instance.GetConfig<T>());
        }

        public InnerApiConfig InnerApiConfigs { get; set; } //= TEC.Public.Infrastructure.Config.ConfigHelper.Instance.GetConfig<InnerApiConfig>();

        public Task<AopServerConfig> GetInnerServerApiConfig(string appid)
        {
            if (InnerApiConfigs == null)
            {
                InnerApiConfigs = TEC.Public.Infrastructure.Config.ConfigHelper.Instance.GetConfig<InnerApiConfig>();
                //throw new Exception($"配置文件 InnerApiConfig 读取出错");
            }
            if (InnerApiConfigs.ServerApps == null || InnerApiConfigs.ServerApps.Count == 0)
            {
                throw new Exception($"配置文件 InnerApiConfig.ServerApps 没有配置项");
            }
            else
            {
                var configs = InnerApiConfigs.ServerApps.Where(x => string.Compare(x.APP_ID, appid, true) == 0);
                if (configs.Count() != 1)
                {
                    throw new Exception($"配置文件 InnerApiConfig.ServerApps 没有找到配置APP_ID:{appid}项,或者出现APP_ID:{appid}重复项");
                }
                var config = configs.FirstOrDefault();

                return Task.FromResult(config);
            }
        }

        public Task<AopClientConfig> GetInnerClientApiConfig(string appid)
        {
            if (InnerApiConfigs == null)
            {
                InnerApiConfigs = TEC.Public.Infrastructure.Config.ConfigHelper.Instance.GetConfig<InnerApiConfig>();
                //throw new Exception($"配置文件 InnerApiConfig 读取出错");
            }
            if (InnerApiConfigs.ClientApps == null || InnerApiConfigs.ClientApps.Count == 0)
            {
                throw new Exception($"配置文件 InnerApiConfig.ClientApps 没有配置项");
            }
            else
            {
                var configs = InnerApiConfigs.ClientApps.Where(x => string.Compare(x.APP_ID, appid, true) == 0);
                if (configs.Count() != 1)
                {
                    throw new Exception($"配置文件 InnerApiConfig.ClientApps 没有找到配置APP_ID:{appid}项,或者出现APP_ID:{appid}重复项");
                }
                var config = configs.FirstOrDefault();

                return Task.FromResult(config);
            }
        }
    }
}
